package it.amicosmanettone.whatsimage.adapter;

import it.amicosmanettone.whatsimage.MainActivity;
import it.amicosmanettone.whatsimage.R;
import it.amicosmanettone.whatsimage.client.ApiClient;
import android.content.Context;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
 
public class MainGridAdapter extends ArrayAdapter {
	
	private Context mContext;
	
    public MainGridAdapter(Context context, int textViewResourceId) {
		super(context, textViewResourceId);
		this.mContext = context;
	}
 
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
    	LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		convertView = inflater.inflate(R.layout.row_main, null);
		
		Typeface champagne = Typeface.createFromAsset(mContext.getAssets(), "champagne.ttf");
		
		final SquareImageView imageCategory = (SquareImageView)convertView.findViewById(R.id.main_image_category);
		
		imageCategory.setBackgroundResource(R.drawable.download_image);
		imageCategory.setPadding(80, 80, 80, 80);
    	
        TextView nameCategory = (TextView) convertView.findViewById(R.id.main_name_category);
        
        nameCategory.setText(MainActivity.CURRENT_CATEGORY_LIST.get(position).getCategoryName());
        nameCategory.setTypeface(champagne);
        nameCategory.refreshDrawableState();
        
        String urlImage = MainActivity.CURRENT_CATEGORY_LIST.get(position).getCategoryImage();
        
        Log.v("URL",urlImage);
        Log.v("ImageDownloader", "Scarico immagine");
        boolean downloadImage = ApiClient.getImage(imageCategory, urlImage);
        imageCategory.setPadding(0, 0, 0, 0);
        Log.v("ImageDownloader", "Finito scaricamento immagine");
        if(!downloadImage){
        
        	Log.v("IMM","SETTO SE NON PRESENTE");
        	Drawable res = mContext.getResources().getDrawable(R.drawable.no_image);
        	imageCategory.setImageDrawable(res);
        	
        	
        }
        
        return convertView;
    }
 
}